package com.example.demo.Util;



import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class MyUtil {

	public static  String createVerify()
	{
		Random random=new Random();
		return Integer.toString(Math.abs(random.nextInt())%1000000);
	}


	public static String toChinese(String str)
	{	String temp="";
		try {
			temp=new String(str.getBytes("iso-8859-1"),"utf-8");
			return temp;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return temp;
	}
	public  static  long createRandom(int n){
		String str="";
		long temp=0;
		do {
			temp=10;
		while (--n>0){
			temp*=10;
		}
			temp=(long) (Math.random()*temp);
			str=Long.toString(temp);
		}while (str.length()==n);
		return temp;
	}

	/**
	 * 获取随机字符和数字
	 * @param length
	 * @return
	 */
	public static String getRandomCharAndNumr(Integer length) {
		String str = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			boolean b = random.nextBoolean();
			if (b) {
				str += (char) (65 + random.nextInt(26));
			} else {
				str += String.valueOf(random.nextInt(10));
			}
		}
		return str;
	}
	public synchronized static  String  createMark() {
		Date date=new Date();
		DateFormat df=new SimpleDateFormat("yyMMddhhmm");
		String str1=Long.toString(Long.parseLong(df.format(date)),36).toUpperCase();
		String str2=Long.toString(MyUtil.createRandom(2),36).toString().toUpperCase();
		return  str1+str2;
	}

	/**
	 * 获取ip地址
	 * @param request
	 * @return
	 */
	public static String getRemoteHost(HttpServletRequest request){
		String ip = request.getHeader("x-forwarded-for");
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getHeader("Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
			ip = request.getRemoteAddr();
		}
		return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
	}
	public static Cookie getCookieByName(HttpServletRequest request, String name){
		Map<String, Cookie> cookieMap = ReadCookieMap(request);
		if(cookieMap.containsKey(name)){
			Cookie cookie = (Cookie)cookieMap.get(name);
			return cookie;
		}else{
			return null;
		}
	}

	private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request){
		Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
		Cookie[] cookies = request.getCookies();
		if(null!=cookies){
			for(Cookie cookie : cookies){
				cookieMap.put(cookie.getName(), cookie);
			}
		}
		return cookieMap;
	}

	/**
	 * 创建32位md5加密
	 * @param plainText
	 * @return
	 */
	public static String createMD5(String plainText) {
		String re_md5 = new String();
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			re_md5 = buf.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return re_md5;
	}

	public static int sendSMS(String code, String phone, String type) {
		System.out.println("----------------------------------");
		System.err.println("发送验证码:" + code + "至" + phone);
		return 0;
	}
}
